home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
2_2.lha
/
2_2
/
2_2_sqrt.c
next >
Wrap
Text File
|
1993-08-08
|
966b
|
49 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
include <complex.h>
include <math.h>
/ determine sqrt of x + yi
omplex sqrt(complex z)
double x = real(z);
double y = imag(z);
// the easy ones: y == 0
if (y == 0.0)
if (x < 0.0)
return complex(0.0, sqrt(-x));
else
return complex(sqrt(x), 0.0);
// almost as easy: x == 0
if (x == 0.0)
if (y < 0.0)
{
double x = sqrt(-y / 2);
return complex(x, -x);
}
else
{
double x = sqrt(y / 2);
return complex(x, x);
}
// convert to polar and take the root
//
// 2 2 1/2
// r = ( x + y )
//
// theta = O- = arc tan (y / x)
//
// 1/2 1/2
// z = r (cos O-/2 + i sin O-/2)
double root_r = sqrt(sqrt(x * x + y * y));
double half_t = atan2(y, x) / 2.0;
return complex(root_r * cos(half_t),
root_r * sin(half_t));